﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    public sealed class CKeyFrame
    {

        #region Fields

        //private CTransform[] m_poses;
        private List<CTransform> m_pose;

        #endregion

        #region Property

        public TimeSpan Time { get; set; }

        [ContentSerializer]
        public List<CTransform> Pose
        {
            get { return this.m_pose; }
            set
            {
                if (this.m_pose != value)
                {
                    //this.m_poses = value.ToArray();
                    this.m_pose = value;
                }
            }
        }

        //public CTransform[] Pose
        //{
        //    get { return this.m_poses; }
        //}

        #endregion

        #region CTOR

        public CKeyFrame()
        {

        }

        public CKeyFrame(TimeSpan _time)
        {
            Time = _time;
            m_pose = new List<CTransform>();
        }

        #endregion

        #region Method

        public CKeyFrame AddPose(Matrix transfom)
        {
            this.m_pose.Add(new CTransform(transfom));
            return this;
        }

        public CKeyFrame AddPose(Quaternion _rot, Vector3 _pos, float _scale)
        {
            this.m_pose.Add(new CTransform(_rot, _pos, _scale));
            return this;
        }

        public CKeyFrame AddPose(CTransform _trans)
        {
            this.m_pose.Add(_trans);
            return this;
        }

        public CKeyFrame AddPose(IEnumerable<CTransform> _trans)
        {
            this.m_pose.AddRange(_trans);
            return this;
        }

        public List<CTransform> Lerp(CKeyFrame _keyframe, float value)
        {
            List<CTransform> _transforms = new List<CTransform>();
            int _size = m_pose.Count;
            for (int i = 0; i != _size; ++i)
            {
                if (m_pose[i] == null)
                    _transforms.Add(null);
                else if (_keyframe.Pose[i] == null)
                    _transforms.Add(m_pose[i]);
                else // TODO : a verifier
                    _transforms.Add(CTransform.Lerp(m_pose[i], _keyframe.Pose[i], value));
            }
            return _transforms;
        }

        public void RemoveTransform(int index)
        {
            this.m_pose.RemoveAt(index);
        }
        #endregion
    }
}
